package cn.e4j.modular.crm.service.impl;

import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.DateUtils;
import cn.e4j.core.util.InvokeUtil;
import cn.e4j.core.util.StringUtils;
import cn.e4j.modular.common.service.SysAuthorityService;
import cn.e4j.modular.crm.entity.GwepT;
import cn.e4j.modular.crm.entity.GweqT;
import cn.e4j.modular.crm.mapper.Gwei022Mapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 回款配票 服务实现类
 * </p>
 *
 * @author miaoyl
 * @since 2019-08-08
 */
@Service
public class Gwei022ServiceImpl extends ServiceImpl<Gwei022Mapper, GwepT> {

    @Autowired
    public SysAuthorityService sysAuthorityService;

    @Transactional(readOnly = true)
    public List<Dto> getListData(Page<Object> page, Dto dto) {
//        List<Dto> gwepDtos = this.baseMapper.getList(page, dto);
        List<Dto> gwepDtos = InvokeUtil.invokeBeanMethod("gwei022Mapper,getList",page,dto);
        return gwepDtos;
    }

    @Transactional(readOnly = true)
    public Dto getGwep010(Dto dto) {
//        Dto gwep010Dtos = this.baseMapper.getGwep010(dto);
        Dto gwep010Dtos = InvokeUtil.invokeBeanMethod("gwei022Mapper,getGwep010",dto);
        return gwep010Dtos;
    }

    @Transactional(readOnly = true)
    public List<Dto> listOrg(Dto dto) {
        dto.put("IS_DEL", "0");
        dto.put("TYPE","2");
//        List<Dto> orgDtos = this.baseMapper.listOrg(dto);
        List<Dto> orgDtos = InvokeUtil.invokeBeanMethod("gwei022Mapper,listOrg",dto);
        return orgDtos;
    }

    @Transactional(readOnly = true)
    public List<Dto> getGwakData(Page<Object> page, Dto dto) {
        //客户权限
        dto.put("CUSTOMERS", sysAuthorityService.getCustomersAuthority(dto));
        List<Dto> gwakDtos = this.baseMapper.getGwakPage(page, dto);
        return gwakDtos;
    }

    @Transactional(readOnly = true)
    public List<Dto> getGweoPage(Page<Object> page, Dto dto) {
        if(! StringUtils.isEmpty(dto.getString("STR"))){
            String[] str = dto.getString("STR").split(",");
            List<String> plist = Lists.newArrayList();
            for (String gweo : str) {
                plist.add(gweo);
            }
            dto.put("GWEO",plist);
        }
//        List<Dto> gweoDtos = this.baseMapper.getGweoPage(page, dto);
        List<Dto> gweoDtos = InvokeUtil.invokeBeanMethod("gwei022Mapper,getGweoPage",page,dto);
        return gweoDtos;
    }

    @Transactional(readOnly = true)
    public Dto getGwak(Dto dto) {
        Dto gwakDtos = this.baseMapper.getGwak(dto);
        return gwakDtos;
    }

    @Transactional(readOnly = true)
    public List<Dto> getGwefPage(Page<Object> page, Dto dto) {
        if(! StringUtils.isEmpty(dto.getString("STR"))){
            String[] str = dto.getString("STR").split(",");
            List<String> plist = Lists.newArrayList();
            for (String gwef : str) {
                plist.add(gwef);
            }
            dto.put("GWEF",plist);
        }
//        List<Dto> gwefDtos = this.baseMapper.getGwefPage(page, dto);
        List<Dto> gwefDtos = InvokeUtil.invokeBeanMethod("gwei022Mapper,getGwefPage",page,dto);
        return gwefDtos;
    }

    /**
     * 回款配票新增
     */
    @Transactional
    public void addData(List<GwepT> gwepTList, List<GweqT> gweqTList, GwepT gwepT) {
        ShiroUser shiroUser = ShiroKit.getUser();
        //增加新的明细
        for (int i = 0; i < gwepTList.size(); i++) {
            GwepT gwep = gwepTList.get(i);
            gwep.setGWEPENT(shiroUser.getDBC());
            gwep.setGWEPACTI("N");
            gwep.setGWEPSEQ(new BigDecimal(i+1));
            gwep.setGWEPLEGAL(gwep.getGWEPPLANT());
            gwep.setGWEPUSER(shiroUser.getACCOUNT());
            gwep.setGWEPGRUP(shiroUser.getORG_ID());
            gwep.setGWEPDATE(LocalDate.now());
            gwep.setGWEPTIME(DateUtils.formatDate(new Date(), "HH:mm:ss"));
            gwep.setGWEPORIG(shiroUser.getORG_ID());
            gwep.setGWEPORIU(shiroUser.getACCOUNT());
            gwep.insert();
        }
        String GWEQDOCNO = DateUtils.getDate("yyyyMMddHHmmssSSSSS");
        for (int i = 0; i < gweqTList.size(); i++) {
            GweqT gweq = gweqTList.get(i);
            gweq.setGWEQENT(shiroUser.getDBC());
            gweq.setGWEQDOCNO(GWEQDOCNO);
            gweq.setGWEQACTI("N");
            gweq.setGWEQ103(gwepT.getGWEPDOCNO());
            gweq.setGWEQDOCDATE(gwepT.getGWEPDOCDATE());
            gweq.setGWEQ008(gwepT.getGWEP003());
            gweq.setGWEQ009(gwepT.getGWEP004());
            gweq.setGWEQ009(gwepT.getGWEP004());
            gweq.setGWEQ010(gwepT.getGWEP005());
            gweq.setGWEQ010(gwepT.getGWEP005());
            gweq.setGWEQACTI("N");
            gweq.setGWEQPLANT(gwepT.getGWEPPLANT());
            gweq.setGWEQLEGAL(gwepT.getGWEPPLANT());
            gweq.setGWEQUSER(shiroUser.getACCOUNT());
            gweq.setGWEQGRUP(shiroUser.getORG_ID());
            gweq.setGWEQDATE(LocalDate.now());
            gweq.setGWEQTIME(DateUtils.formatDate(new Date(), "HH:mm:ss"));
            gweq.setGWEQORIG(shiroUser.getORG_ID());
            gweq.setGWEQORIU(shiroUser.getACCOUNT());
            gweq.insert();
        }
    }

    /**
     * 回款配票修改
     */
    @Transactional
    public void editData(List<GwepT> gwepTList, List<GweqT> gweqTList, GwepT gwepT) {
        ShiroUser shiroUser = ShiroKit.getUser();
        //删除明细
        UpdateWrapper<GwepT> gwepTUpdateWrapper = new UpdateWrapper<>();
        gwepTUpdateWrapper.eq("GWEPENT",shiroUser.getDBC());
        gwepTUpdateWrapper.eq("GWEPDOCNO",gwepT.getGWEPDOCNO());
        GwepT delGwep = new GwepT();
        delGwep.delete(gwepTUpdateWrapper);

        UpdateWrapper<GweqT> gweqTUpdateWrapper = new UpdateWrapper<>();
        gweqTUpdateWrapper.eq("GWEQENT",shiroUser.getDBC());
        gweqTUpdateWrapper.eq("GWEQ103",gwepT.getGWEPDOCNO());
        GweqT delGweq = new GweqT();
        delGweq.delete(gweqTUpdateWrapper);

        //增加新的明细
        for (int i = 0; i < gwepTList.size(); i++) {
            GwepT gwep = gwepTList.get(i);
            gwep.setGWEPENT(shiroUser.getDBC());
            gwep.setGWEPACTI("N");
            gwep.setGWEPSEQ(new BigDecimal(i+1));
            gwep.setGWEPLEGAL(gwep.getGWEPPLANT());
            gwep.setGWEPUSER(shiroUser.getACCOUNT());
            gwep.setGWEPGRUP(shiroUser.getORG_ID());
            gwep.setGWEPDATE(LocalDate.now());
            gwep.setGWEPTIME(DateUtils.formatDate(new Date(), "HH:mm:ss"));
            gwep.setGWEPORIG(shiroUser.getORG_ID());
            gwep.setGWEPORIU(shiroUser.getACCOUNT());
            gwep.insert();
        }
        String GWEQDOCNO = DateUtils.getDate("yyyyMMddHHmmssSSSSS");
        for (int i = 0; i < gweqTList.size(); i++) {
            GweqT gweq = gweqTList.get(i);
            gweq.setGWEQENT(shiroUser.getDBC());
            gweq.setGWEQDOCNO(GWEQDOCNO);
            gweq.setGWEQACTI("N");
            gweq.setGWEQ103(gwepT.getGWEPDOCNO());
            gweq.setGWEQDOCDATE(gwepT.getGWEPDOCDATE());
            gweq.setGWEQ008(gwepT.getGWEP003());
            gweq.setGWEQ009(gwepT.getGWEP004());
            gweq.setGWEQ009(gwepT.getGWEP004());
            gweq.setGWEQ010(gwepT.getGWEP005());
            gweq.setGWEQ010(gwepT.getGWEP005());
            gweq.setGWEQACTI("N");
            gweq.setGWEQPLANT(gwepT.getGWEPPLANT());
            gweq.setGWEQLEGAL(gwepT.getGWEPPLANT());
            gweq.setGWEQUSER(shiroUser.getACCOUNT());
            gweq.setGWEQGRUP(shiroUser.getORG_ID());
            gweq.setGWEQDATE(LocalDate.now());
            gweq.setGWEQTIME(DateUtils.formatDate(new Date(), "HH:mm:ss"));
            gweq.setGWEQORIG(shiroUser.getORG_ID());
            gweq.setGWEQORIU(shiroUser.getACCOUNT());
            gweq.insert();
        }
    }

    @Transactional
    public void delete(GwepT gwepT ) {
        ShiroUser shiroUser = ShiroKit.getUser();
        UpdateWrapper<GwepT> gwepTUpdateWrapper = new UpdateWrapper<>();
        gwepTUpdateWrapper.eq("GWEPENT", shiroUser.getDBC());
        gwepTUpdateWrapper.eq("GWEPDOCNO", gwepT.getGWEPDOCNO());
        gwepT.delete(gwepTUpdateWrapper);

        GweqT gweqT = new GweqT();
        UpdateWrapper<GweqT> gweqTUpdateWrapper = new UpdateWrapper<>();
        gweqTUpdateWrapper.eq("GWEQENT", shiroUser.getDBC());
        gweqTUpdateWrapper.eq("GWEQ103", gwepT.getGWEPDOCNO());
        gweqT.delete(gweqTUpdateWrapper);
    }

    @Transactional
    public void delDataBatch(List<GwepT> gwepTList) {
        ShiroUser shiroUser = ShiroKit.getUser();
        for (GwepT gwepT : gwepTList) {
            UpdateWrapper<GwepT> gwepTUpdateWrapper = new UpdateWrapper<>();
            gwepTUpdateWrapper.eq("GWEPENT", shiroUser.getDBC());
            gwepTUpdateWrapper.eq("GWEPDOCNO", gwepT.getGWEPDOCNO());
            gwepT.delete(gwepTUpdateWrapper);

            GweqT gweqT = new GweqT();
            UpdateWrapper<GweqT> gweqTUpdateWrapper = new UpdateWrapper<>();
            gweqTUpdateWrapper.eq("GWEQENT", shiroUser.getDBC());
            gweqTUpdateWrapper.eq("GWEQ103", gwepT.getGWEPDOCNO());
            gweqT.delete(gweqTUpdateWrapper);
        }
    }

    @Transactional(readOnly = true)
    public List<Dto> getGwepData(Dto dto) {
//        List<Dto> gwepDtos = this.baseMapper.getGwepData(dto);
        List<Dto> gwepDtos = InvokeUtil.invokeBeanMethod("gwei022Mapper,getGwepData",dto);
        return gwepDtos;
    }

    @Transactional(readOnly = true)
    public List<Dto> getGweqData(Dto dto) {
//        List<Dto> gweqDtos = this.baseMapper.getGweqData(dto);
        List<Dto> gweqDtos = InvokeUtil.invokeBeanMethod("gwei022Mapper,getGweqData",dto);
        return gweqDtos;
    }

    @Transactional
    public void updateStatus(List<GwepT> gwepTList,Dto inDto) {
        ShiroUser shiroUser = ShiroKit.getUser();
        for (GwepT gwepT : gwepTList) {
            gwepT.setGWEPACTI(inDto.getString("ACTI"));
            UpdateWrapper<GwepT> gwepTUpdateWrapper = new UpdateWrapper<>();
            gwepTUpdateWrapper.eq("GWEPENT", shiroUser.getDBC());
            gwepTUpdateWrapper.eq("GWEPDOCNO", gwepT.getGWEPDOCNO());
            gwepTUpdateWrapper.eq("GWEPACTI", inDto.getString("ACTI_OLD"));
            gwepT.update(gwepTUpdateWrapper);

            GweqT gweqT = new GweqT();
            gweqT.setGWEQACTI(inDto.getString("ACTI"));
            UpdateWrapper<GweqT> gweqTUpdateWrapper = new UpdateWrapper<>();
            gweqTUpdateWrapper.eq("GWEQENT", shiroUser.getDBC());
            gweqTUpdateWrapper.eq("GWEQ103", gwepT.getGWEPDOCNO());
            gweqTUpdateWrapper.eq("GWEQACTI", inDto.getString("ACTI_OLD"));
            gweqT.update(gweqTUpdateWrapper);
        }
    }

    @Transactional(readOnly = true)
    public List<Dto> getGwecdocno(Page<Object> page, Dto dto) {
//        List<Dto> gweqDtos = this.baseMapper.getGwecdocno(page, dto);
        List<Dto> gweqDtos = InvokeUtil.invokeBeanMethod("gwei022Mapper,getGwecdocno",page,dto);
        return gweqDtos;
    }

}
